package com.sumainfor.wx.tenant.controller;

import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.wx.tenant.feign.EpcFeignClient;
import com.sumainfor.wx.tenant.feign.MarketFeignClient;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  在线保修
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-04-16 20:01
 **/
@Controller
@RequestMapping("/maintenanceApplication")
public class MaintenanceApplicationController {

    @Autowired
    EpcFeignClient epcFeignClient;

    @Autowired
    MarketFeignClient marketFeignClient;


    /**
     * <p>
     *查询受理中的维修工单
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @ResponseBody
    @PostMapping("/toAccept")
    public JsonResult toAccept(@RequestBody Map<String,Object> params){

        return epcFeignClient.toAccept(params);
    }

    /**
     * <p>
     *查询待上门
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @ResponseBody
    @PostMapping("/forTheDoor")
    public JsonResult forTheDoor(@RequestBody Map<String,Object> params){
        return epcFeignClient.forTheDoor(params);
    }

    /**
     * <p>
     *查询待支付
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @ResponseBody
    @PostMapping("/toBePaid")
    public JsonResult toBePaid(@RequestBody Map<String,Object> params){

        return epcFeignClient.toBePaid(params);
    }

    /**
     * <p>
     * 查询待评价
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @ResponseBody
    @PostMapping("/toEvaluate")
    public JsonResult toEvaluate(@RequestBody Map<String,Object> params){
        return epcFeignClient.toEvaluate(params);
    }

    /**
     * <p>
     *查询已完成
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @ResponseBody
    @PostMapping("/hasBeenCompleted")
    public JsonResult hasBeenCompleted(@RequestBody Map<String,Object> params){
        return epcFeignClient.hasBeenCompleted(params);
    }


    /**
    * <p>
    * 查询维修师傅的评价
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/getStarAvg")
    public JsonResult getStarAvg(@RequestBody Map<String,Object> params){
        return epcFeignClient.getStarAvg(params);
    }

    /**
    * <p>
    * 取消预约
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/deleteTcepcRepair")
    public JsonResult deleteTcepcRepair(@RequestBody Map<String,Object> params){
        return epcFeignClient.deleteTcepcRepair(params);
    }

    /**
    * <p>
    * 增加维修评价
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/insertRepairEva")
    public JsonResult insertRepairEva(@RequestBody Map<String,Object> params){
        return epcFeignClient.insertRepairEva(params);

    }


      /**
      * <p>
      * 获取报修物品清单-分类
      * </p>
      *
      * @author: zhoucong
      * @since:
      */
    @ResponseBody
    @PostMapping("/getTcepcArticleType")
    public  JsonResult getTcepcArticleType(@RequestBody Map<String,Object> params){
        return epcFeignClient.getTcepcArticleType();
    }

     /**
     * <p>
     * 获取报修物品清单，根据报修物品清单分类编号
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @ResponseBody
    @PostMapping("/getTcepcArtice")
    public JsonResult getTcepcArtice(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ArtTypeId"})){
            return  result.putFailed("报修物品分类编号不能为空！");
        }
        return epcFeignClient.getTcepcArtice(params);
    }

    /**
    * <p>
    * 根据报修物品编号获取物品故障描述信息
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/getTcepcArticleTrouble")
    public JsonResult getTcepcArticleTrouble(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ArtId"})){
            return  result.putFailed("报修物品编号不能为空!");
        }
        return epcFeignClient.getTcepcArticleTrouble(params);
    }

    /**
    * <p>
    * 根据租客id查询房源信息
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/getTenantHousing")
    public JsonResult getTenantHousing(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            return  result.putFailed("租客编号不能为空!");
        }
        //判断房源是否预销，隐藏预销的房源
        JsonResult marketFeignresult = marketFeignClient.getTenantHousing(params);
        List<Map<String,Object>> markFeignList = (List<Map<String, Object>>) marketFeignresult.get("result");
        if (markFeignList!=null&&markFeignList.size()>0){
            for (int i =0;i<markFeignList.size();i++){
                if (MapUtils.getString(markFeignList.get(i),"HouCon")==null||MapUtils.getString(markFeignList.get(i),"HouCon").length()==0){
                    markFeignList.remove(i);
                }
            }

        }
        return result.put(markFeignList);

    }

    /**
    * <p>
    * 根据保修信息提交申请
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/insertTcepcRepair")
    public JsonResult insertTcepcRepair(@RequestBody Map<String,Object> params){
        return epcFeignClient.insertTcepcRepair(params);
    }


}
